package com.rc.adapter;

import java.util.ArrayList;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.TextView;

import com.rc.activity.R;
import com.rc.activity.dao.tbl_Item;

public class AddExpenseItemsAdpter extends BaseAdapter {

	private Context mContext;
	private ArrayList<tbl_Item> items;
	private boolean isCheck = false;
	private OnCheckedChangeListener changeListener;

	public AddExpenseItemsAdpter(Context c, ArrayList<tbl_Item> items,
			OnCheckedChangeListener changeListener) {
		mContext = c;
		this.items = items;
		this.changeListener = changeListener;
	}

	public int getCount() {
		return items.size();
	}

	public Object getItem(int position) {
		return items.get(position);
	}

	public long getItemId(int position) {
		return 0;
	}

	private View v;
	private tbl_Item objItem;
	private CheckBox cbMulti;

	// create a new view for each item referenced by the Adapter
	public View getView(final int position, View convertView, ViewGroup parent) {
		v = convertView;
		TextView txtTitle, txtAmount;
		if (convertView == null) { // if it's not recycled, initialize some
									// attributes
			LayoutInflater vi = (LayoutInflater) mContext
					.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			v = vi.inflate(R.layout.trip_add_expense_item_child, null);
		}
		txtTitle = (TextView) v.findViewById(R.id.txtTitle);
		txtAmount = (TextView) v.findViewById(R.id.txtAmount);
		cbMulti = (CheckBox) v.findViewById(R.id.cbMulti);

		objItem = new tbl_Item();
		objItem = items.get(position);
		txtTitle.setText(objItem.getName());
		txtAmount.setText(objItem.getAmount() + "");
		cbMulti.setChecked(objItem.isCheck());

		if (items.get(position).isCheck()) {
			cbMulti.setChecked(true);
		} else {
			cbMulti.setChecked(false);
		}

		v.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub

				tbl_Item objItem = new tbl_Item();
				objItem = items.get(position);
				cbMulti = null;
				cbMulti = ((CheckBox) v.findViewById(R.id.cbMulti));
				cbMulti.setOnCheckedChangeListener(changeListener);
				boolean isCheck = cbMulti.isChecked() ? false : true;
				objItem.setCheck(isCheck);
				cbMulti.setTag(items.get(position));
				cbMulti.setChecked(isCheck);

			}
		});

		return v;
	}

	public void NotiDB(ArrayList<tbl_Item> items, boolean isCheck) {
		this.items = items;
		this.isCheck = isCheck;
		notifyDataSetChanged();
	}
}